using Circus.CodeEditor;
using Circus.CodeEditor.Serialization;
using Circus.CodeEditor.TextSource;
using Circus.Syntax;
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.CompilerServices;
namespace Circus.CodeEditor
{
	public class EditHyperText {
		private Color urlColor = EditConsts.DefaultUrlForeColor;
		private FontStyle urlStyle = EditConsts.DefaultUrlFontStyle;
		private bool showHints = true;
		private UrlJumpEventArgs urlJumpArgs;
		private CodeEditor owner;

	    [Browsable(false)]
	    public event UrlJumpEvent JumpToUrl;
		[Browsable(false)]
		public event HyperTextEvent HyperText
		{
			add
			{
				this.owner.Source.HyperText += value;
			}
			remove
			{
				this.owner.Source.HyperText -= value;
			}
		}
		[DefaultValue(true), Description("Gets or sets value indicating whether default hint for hypertext section needs displaying when user moves mouse over the hypertext.")]
		public virtual bool ShowHints
		{
			get
			{
				return this.showHints;
			}
			set
			{
				if (this.showHints != value)
				{
					this.showHints = value;
					this.OnShowHintsChanged();
				}
			}
		}
		[Description("Gets or sets font style of highlighted urls.")]
		public virtual FontStyle UrlStyle
		{
			get
			{
				return this.urlStyle;
			}
			set
			{
				if (this.urlStyle != value)
				{
					this.urlStyle = value;
					this.OnUrlStyleChanged();
				}
			}
		}
		[Description("Gets or sets a value that represents color of highlighted urls.")]
		public virtual Color UrlColor
		{
			get
			{
				return this.urlColor;
			}
			set
			{
				if (this.urlColor != value)
				{
					this.urlColor = value;
					this.OnUrlColorChanged();
				}
			}
		}
		
		[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public Hashtable UrlTable
		{
			get
			{
				return this.owner.Source.UrlTable;
			}
		}
		[DefaultValue(false), Description("Gets or sets a value indicating whether hypertext urls in the text should be highlighted.")]
		public virtual bool HighlightHyperText
		{
			get
			{
				return this.owner.Source.HighlightHyperText;
			}
			set
			{
				this.owner.Source.HighlightHyperText = value;
			}
		}
		public EditHyperText()
		{
			this.urlJumpArgs = new UrlJumpEventArgs("", false);
		}
		public EditHyperText(CodeEditor owner) : this()
		{
			this.owner = owner;
		}
		public bool ShouldSerializeUrlColor()
		{
			return this.urlColor != EditConsts.DefaultUrlForeColor;
		}
		public bool ShouldSerializeUrlStyle()
		{
			return this.urlStyle != EditConsts.DefaultUrlFontStyle;
		}
		protected virtual void OnShowHintsChanged()
		{
		}
		protected virtual void OnUrlStyleChanged()
		{
			if (this.HighlightHyperText)
			{
				this.owner.Invalidate();
			}
		}
		protected virtual void OnUrlColorChanged()
		{
			if (this.HighlightHyperText)
			{
				this.owner.Invalidate();
			}
		}
		public virtual void Assign(EditHyperText source)
		{
			this.HighlightHyperText = source.HighlightHyperText;
			this.UrlColor = source.UrlColor;
			this.UrlStyle = source.UrlStyle;
		}
		public virtual void UrlJump(string text)
		{
			this.urlJumpArgs.Text = text;
			this.urlJumpArgs.Handled = false;
			if (this.JumpToUrl != null)
			{
				this.JumpToUrl(this, this.urlJumpArgs);
			}
			if (!this.urlJumpArgs.Handled)
			{
				try
				{
					Process.Start(text);
				}
				catch
				{
				}
			}
		}
		public virtual void ResetUrlStyle()
		{
			this.UrlStyle = EditConsts.DefaultUrlFontStyle;
		}
		public virtual void ResetUrlColor()
		{
			this.UrlColor = EditConsts.DefaultUrlForeColor;
		}
		public virtual void ResetShowHints()
		{
			this.ShowHints = true;
		}
		protected virtual bool IsUrlAtTextPoint(int x, int y, out string url, bool needUrl)
		{
			bool flag = false;
			url = string.Empty;
			if (this.HighlightHyperText)
			{
				StringItem item = this.owner.Lines.GetItem(y);
				if (item != null)
				{
					StringItemInfo[] textData = item.TextData;
					flag = (x >= 0 && x < textData.Length && (byte)(textData[x].TextStyle & TextStyle.HyperText) != 0);
					if (flag && needUrl)
					{
						int num = x;
						int num2 = x;
						while (num > 0 && (byte)(textData[num - 1].TextStyle & TextStyle.HyperText) != 0)
						{
							num--;
						}
						while (num2 < textData.Length - 1 && (byte)(textData[num2 + 1].TextStyle & TextStyle.HyperText) != 0)
						{
							num2++;
						}
						url = item.String.Substring(num, num2 - num + 1);
					}
				}
			}
			return flag;
		}
		public virtual bool IsUrlAtPoint(int x, int y, out string url, bool needUrl)
		{
			url = string.Empty;
			bool result;
			if (this.HighlightHyperText)
			{
				Point point = this.owner.ScreenToText(x, y);
				result = this.IsUrlAtTextPoint(point.X, point.Y, out url, needUrl);
			}
			else
			{
				result = false;
			}
			return result;
		}
		public virtual bool IsHyperText(string text)
		{
			return this.owner.Source.IsHyperText(text);
		}
		public virtual void ResetHighlightHyperText()
		{
			this.HighlightHyperText = false;
		}
		public virtual bool IsUrlAtPoint(int x, int y)
		{
			string text;
			return this.IsUrlAtPoint(x, y, out text, false);
		}
		public virtual bool IsUrlAtPoint(int x, int y, out string url)
		{
			return this.IsUrlAtPoint(x, y, out url, true);
		}
		public virtual bool IsUrlAtTextPoint(int x, int y, out string url)
		{
			return this.IsUrlAtTextPoint(x, y, out url, true);
		}
	}
}
